﻿Imports System.Windows.Forms

Public Class ShipmentDetails
    Inherits dbiWSF.Dialogs.dlgDefault

    Private l_OriginalShipment As dbiWSF.FrameworkObjects.objShipment
    Private l_AdjustedShipment As New dbiWSF.FrameworkObjects.objShipment(myWSFObjects)


    Public Sub New(ByRef curShipment As dbiWSF.FrameworkObjects.objShipment)

        ' This call is required by the Windows Form Designer.
        InitializeComponent()

        'Set the local variable to carry the shipment passed in when the form is created.
        l_OriginalShipment = curShipment

        'Populate the controls on the form with the values of the corresponding properties of the supplied shipment object.
        Me.TextBoxCarrierID.Text = l_OriginalShipment.CarrierID
        Me.TextBoxDescription.Text = l_OriginalShipment.Description
        Me.TextBoxEquipmentID.Text = l_OriginalShipment.EquipmentID
        Me.TextBoxLoadingDockID.Text = l_OriginalShipment.LoadingDockID.ToString
        Me.TextBoxNotes.Text = l_OriginalShipment.Notes
        Me.TextBoxShipmentID.Text = l_OriginalShipment.ShipmentID.ToString

        'Populate the Shipment Type combo box with the shipment types enum values.
        Me.ComboBoxShipmentType.DataSource = System.Enum.GetValues(GetType(dbiWSF.FrameworkObjects.ShipmentTypes))

        'Set the selected item in the combo box to the shipment type of the shipment passed in.
        Me.ComboBoxShipmentType.SelectedItem = l_OriginalShipment.ShipmentType

        'Set the breakout dock quantity to the value on the shipment passed in.
        Me.NumericUpDownBreakOutQty.Value = l_OriginalShipment.BreakoutDockQty
        Me.NumericUpDownPriority.Value = l_OriginalShipment.Priority

        'Set the start and end time controls to the values of the shipment passed in.
        Me.DbiDateTimeStartTime.DateTimeValue = l_OriginalShipment.SchedStartTime
        Me.DbiDateTimeEndTime.DateTimeValue = l_OriginalShipment.SchedEndTime

        'Store the values of the supplied shipment's properties in a temporary shipment object.
        'Used for resetting the values, if there is an error or the user cancels.
        setShipmentValues(l_AdjustedShipment, l_OriginalShipment)

    End Sub

    Private Sub OK_Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OK_Button.Click
        'Update the temporary copy of the shipment with the values on the form.
        UpdateProperties(l_AdjustedShipment)

        Me.DialogResult = System.Windows.Forms.DialogResult.OK

        'Trigger the business rules passing the temporary shipment (with the updated property values)
        Dim RulesDialog As New dbiWSF.Dialogs.dlgBusinessRule(myBusinessRules)
        RulesDialog.StartProcessSynch(l_AdjustedShipment, dbiWSF.BusinessRulesAndSupport.BusinessRuleObjects.RequestAction.Add)
        Me.DialogResult = RulesDialog.DialogResult

        If Me.DialogResult = Windows.Forms.DialogResult.OK Then

            'Update the shipment with the new values from the form.
            setShipmentValues(l_OriginalShipment, l_AdjustedShipment)

        End If

        Me.Close()

    End Sub

    Private Sub Cancel_Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Cancel_Button.Click
        Me.DialogResult = System.Windows.Forms.DialogResult.Cancel
        Me.Close()
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        'Display a form with a list of available shippers/carriers that can be assigned to a shipment.
        Dim frmShip As New ShippersList

        frmShip.ShowDialog(Me)

        If frmShip.ShipperName.Length > 0 Then
            'The user selected a shipper/carrier from the form.
            Me.TextBoxCarrierID.Text = frmShip.ShipperName

        End If

        frmShip.Dispose()

    End Sub

    Private Sub ButtonAddOrders_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonAddOrders.Click

        'Ensure all of the control changes have been stored in the temporary shipment object.
        UpdateProperties(l_AdjustedShipment)

        'Display a form with all the available orders which can be placed on a shipment.
        'NOTE: If the user adds orders to the shipment the Add Orders form will take care of the updating.
        Dim frmAddOrders As New frmAddOrderHeaders(l_AdjustedShipment, myWSFObjects.GetLoadingDockFromGUID(l_OriginalShipment.LoadingDockID))
        frmAddOrders.ShowDialog(Me)

        frmAddOrders.Dispose()

    End Sub

    Private Sub ButtonViewOrders_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonViewOrders.Click

        'Ensure all of the control changes have been stored in the temporary shipment object.
        UpdateProperties(l_AdjustedShipment)

        'Display a form with all the orders currently on a shipment
        'NOTE: If the user removes orders from the shipment the View Orders form will take care of the updating.
        Dim frmViewOrders As New frmOrdersOnShipment(l_AdjustedShipment, myWSFObjects.GetLoadingDockFromGUID(l_OriginalShipment.LoadingDockID))
        frmViewOrders.ShowDialog(Me)

        frmViewOrders.Dispose()

    End Sub

    Private Sub UpdateProperties(ByVal curShipment As dbiWSF.FrameworkObjects.objShipment)

        'Update the original shipment object's properties with the values from the controls on the form.
        curShipment.CarrierID = Me.TextBoxCarrierID.Text
        curShipment.Description = Me.TextBoxDescription.Text
        curShipment.EquipmentID = Me.TextBoxEquipmentID.Text
        curShipment.Notes = Me.TextBoxNotes.Text
        curShipment.ShipmentType = Me.ComboBoxShipmentType.SelectedItem
        curShipment.BreakoutDockQty = Me.NumericUpDownBreakOutQty.Value
        curShipment.Priority = Me.NumericUpDownPriority.Value
        curShipment.SchedStartTime = Me.DbiDateTimeStartTime.DateTimeValue
        curShipment.SchedEndTime = Me.DbiDateTimeEndTime.DateTimeValue

    End Sub

    Private Sub ComboBoxShipmentType_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs)
        MessageBox.Show("Please note that any orders previously assigned to this shipment have not been removed or reassigned." + _
                        "For the purpose of this demo, no logic has been included to handle this situation.", "The ShipmentType has been changed.", _
                        MessageBoxButtons.OK, MessageBoxIcon.Information)
    End Sub

    Private Sub ShipmentDetails_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        'Event handler for the changing of the shipment type combo box.
        AddHandler Me.ComboBoxShipmentType.SelectedIndexChanged, AddressOf ComboBoxShipmentType_SelectedIndexChanged

    End Sub

    Private Sub setShipmentValues(ByVal ShipmentToChange As dbiWSF.FrameworkObjects.objShipment, ByVal ShipmentToGetValues As dbiWSF.FrameworkObjects.objShipment)
        'Set the properties of a shipment object equal to another shipment object.

        'NOTE:  This method is used to store the values of the supplied shipment in a temporary one when the form is loaded.
        '       This method is again used to update the original shipment with the values of the temporary one once all validation has passed.

        ShipmentToChange.BreakoutDockQty = ShipmentToGetValues.BreakoutDockQty
        ShipmentToChange.CarrierID = ShipmentToGetValues.CarrierID
        ShipmentToChange.Description = ShipmentToGetValues.Description
        ShipmentToChange.EquipmentID = ShipmentToGetValues.EquipmentID
        ShipmentToChange.LoadingDockID = ShipmentToGetValues.LoadingDockID
        ShipmentToChange.Notes = ShipmentToGetValues.Notes
        ShipmentToChange.OrderHeaders = ShipmentToGetValues.OrderHeaders
        ShipmentToChange.Priority = ShipmentToGetValues.Priority
        ShipmentToChange.SchedEndTime = ShipmentToGetValues.SchedEndTime
        ShipmentToChange.SchedStartTime = ShipmentToGetValues.SchedStartTime
        ShipmentToChange.ShipmentID = ShipmentToGetValues.ShipmentID
        ShipmentToChange.ShipmentType = ShipmentToGetValues.ShipmentType
        ShipmentToChange.Tag = ShipmentToGetValues.Tag

    End Sub

End Class
